package com.ruoyi.system.modbus;

import com.serotonin.modbus4j.ModbusMaster;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ModbusController {
    private static final Integer flag = 0;


    public static Boolean readCoilStatus(ModbusMaster master, int slaveId, int offset, String dev_code) {
        synchronized (flag) {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return Modbus4jReadUtil.readCoilStatus(master, slaveId, offset, dev_code);
        }
    }

    public static Boolean writeCoil(ModbusMaster master, int slaveId, int writeOffset, boolean writeValue) {
        synchronized (flag) {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return Modbus4jWriteUtils.writeCoil(master, slaveId, writeOffset, writeValue);
        }
    }

    public static boolean writeCoils(ModbusMaster master, int slaveId, int startOffset, boolean[] bdata) {
        synchronized (flag) {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return Modbus4jWriteUtils.writeCoils(master, slaveId, startOffset, bdata);
        }
    }

    public static Number readInputRegisters(ModbusMaster master, int slaveId, int offset, int dataType, String dev_code) {
        synchronized (flag) {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return Modbus4jReadUtil.readInputRegisters(master, slaveId, offset, dataType, dev_code);
        }
    }

    public static Boolean readInputStatus(ModbusMaster master, int slaveId, int offset, String dev_code) {
        synchronized (flag) {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return Modbus4jReadUtil.readInputStatus(master, slaveId, offset, dev_code);
        }
    }
}
